package com.anquing.ssodemo.redisService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

import javax.annotation.PostConstruct;

/**
 * 单个redis
 */
@Service
public class RedisService {

    private static Logger log = LoggerFactory.getLogger(RedisService.class);

    @Autowired
    private JedisPool pool;

    private static RedisService redisService;

    /**
     * @PostConstruct修饰的方法会在服务器加载Servle的时候运行，并且只会被服务器执行一次
     */
    @PostConstruct
    public void init() {
        redisService = this;
    }



    private static Jedis getJedis() {
        Jedis jedis = null;
        try {
            JedisPool pool = redisService.pool;
            jedis = pool.getResource();
        } catch (JedisConnectionException e) {
            if (jedis != null) {
                jedis.close();
                log.error("getJedis error" + e.getMessage(), e);
            }
        }
        return jedis;
    }

    public static void close(Jedis redis) {
        if (redis != null) {
            redis.close();
        }
    }

    /**
     * 保存(注意，数据如果已经存在则会覆盖),并设置过期时间
     *
     * @param  key	键名
     * @param  value  值
     * @param  seconds	过期时间，单位:秒
     * @return null为添加失败
     */
    public  static String setex(String key, String value,int seconds) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = getJedis();
            result = jedis.setex(key, seconds, value);
        } catch (Exception e) {
            log.error("setex key:{} value:{} seconds:{} error",key,value,seconds,e);
        } finally {
            close(jedis);
        }
        return result;
    }

    /**
     * 设置过期时间
     *
     * @param key
     * @param seconds
     * @return 0:失败，1:成功
     */
    public static Long setExpire(String key, int seconds) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = getJedis();
            result = jedis.expire(key, seconds);
        } catch (Exception e) {
            log.error("setExpire key:{}  error ",key, e);
        } finally {
            close(jedis);
        }
        return result;
    }

    /**
     * 添加数据
     *
     * @param  key
     * @param  value
     * @return null为添加失败
     */
    public static String set(String key,String value) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = getJedis();
            result = jedis.set(key,value);
        } catch (Exception e) {
            log.error("set key:{} value:{} error",key,value,e);
        } finally {
            close(jedis);
        }
        return result;
    }

    /**
     * 获取数据
     *
     * @param  key
     * @return
     */
    public static String get(String key) {
        Jedis jedis = null;
        String value = null;
        try {
            jedis = getJedis();
            value = jedis.get(key);
        } catch (Exception e) {
            log.error("get key:{} error",key,e);
        } finally {
            close(jedis);
        }
        return value;
    }

    /**
     * 删除数据
     *
     * @param  key
     * @return
     */
    public static Long del(String key) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = getJedis();
            result = jedis.del(key);
        } catch (Exception e) {
            log.error("del key:{} error",key,e);
        } finally {
            close(jedis);
        }
        return result;
    }

    /**
     * 添加数据（如果key已存在，内则会添加使失败）
     *
     * @param  key
     * @param  value
     * @return 成功返回1，失败返回0
     */
    public static Long setnx(String key,String value) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = getJedis();
            result = jedis.setnx(key,value);
        } catch (Exception e) {
            log.error("set key:{} value:{} error",key,value,e);
        } finally {
            close(jedis);
        }
        return result;
    }

    /**
     * 更新数据，返回旧的值
     *
     * @param key
     * @param value
     * @return
     */
    public static String getSet(String key,String value){
        Jedis jedis = null;
        String result = null;
        try {
            jedis = getJedis();
            result = jedis.getSet(key,value);
        } catch (Exception e) {
            log.error("getset key:{} value:{} error",key,value,e);
        }
        finally {
            close(jedis);
        }
        return result;
    }
}
